﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GomeeSoft.src.classes;

namespace GomeeSoft.src.control
{
    public partial class UCSelectDay : UserControl
    {
        // Variable ************************************************************************
        #region Variable
        private int year = 2013;
        public int Year
        {
            get { return year; }
            set { year = value; SetYearMonth(); }
        }
        private int month = 1;
        public int Month
        {
            get { return month; }
            set { month = value; SetYearMonth(); }
        }
        private DateTime selectedDate = DateTime.Now;
        public DateTime SelectedDate
        {
            get { return selectedDate; }
            set
            {
                selectedDate = value;
                year = selectedDate.Year;
                month = selectedDate.Month;
                SetYearMonth();
            }
        }

        private Rectangle[] buttonRange = new Rectangle[6];
        private Label lbYear = new Label();
        private Label lbMonth = new Label();
        private Label[] lbWeekDay = new Label[7];
        private Label[,] lbMonthDate = new Label[6, 7];
        #endregion
        
        // Initialize **********************************************************************
        #region Intialize
        public UCSelectDay()
        {
            InitializeComponent();
            InitializeYearMonth();
            InitializeWeekDay();
            InitializeMonthDate();
        }
        private void InitializeYearMonth()
        {
            Font f = new Font(Global.FontTahoma, 8);
            Color c = Global.GetColor(Global.UCButtonForeColor);
            lbYear.Font = f;
            lbYear.BackColor = Color.Transparent;
            lbYear.ForeColor = c;
            lbYear.TextAlign = ContentAlignment.MiddleCenter;
            lbYear.Location = new Point(39, 4);
            lbYear.Size = new Size(34, 16);
            lbMonth.Font = f;
            lbMonth.BackColor = Color.Transparent;
            lbMonth.ForeColor = c;
            lbMonth.TextAlign = ContentAlignment.MiddleCenter;
            lbMonth.Location = new Point(125, 4);
            lbMonth.Size = new Size(20, 16);
            Controls.Add(lbYear);
            Controls.Add(lbMonth);
        }
        private void InitializeWeekDay()
        {
            for (int i=0; i<7; i++)
            {
                Font f = new Font(Global.FontTahoma, 8);
                Color c = Global.GetColor(Global.UCButtonBorderDark);
                Point p = new Point((i * (Width - 4) / 7 + 2), 25);
                Size s = new Size((Width - 4) / 7, 15);
                lbWeekDay[i] = new Label();
                lbWeekDay[i].Font = f;
                lbWeekDay[i].ForeColor = c;
                lbWeekDay[i].Location = p;
                lbWeekDay[i].Size = s;
                lbWeekDay[i].TextAlign = ContentAlignment.MiddleCenter;
                Controls.Add(lbWeekDay[i]);
            }
            lbWeekDay[0].Text = "Sun";
            lbWeekDay[1].Text = "Mon";
            lbWeekDay[2].Text = "Tue";
            lbWeekDay[3].Text = "Wed";
            lbWeekDay[4].Text = "Thu";
            lbWeekDay[5].Text = "Fri";
            lbWeekDay[6].Text = "Sat";
        }
        private void InitializeMonthDate()
        {
            Font f = new Font(Global.FontTahoma, 8);
            for (int row = 0; row < 6; row++)
            {
                for (int col = 0; col < 7; col++)
                {
                    Point p = new Point(col * (Width - 4) / 7 + 2, row * 15 + 44);
                    Size s = new Size((Width - 4) / 7, 15);
                    lbMonthDate[row, col] = new Label();
                    lbMonthDate[row, col].Font = f;
                    lbMonthDate[row, col].Location = p;
                    lbMonthDate[row, col].Size = s;
                    lbMonthDate[row, col].TextAlign = ContentAlignment.MiddleCenter;
                    lbMonthDate[row, col].Click += new System.EventHandler(LBMonthDate_Click);
                    Controls.Add(lbMonthDate[row, col]);
                }
            }
        }
        #endregion

        // Paint ***************************************************************************
        #region Paint
        private void UCSelectDay_Paint(object sender, PaintEventArgs e)
        {
            PaintBorder(sender, e);
            PaintMenuBar(sender, e);
            PaintButton(sender, e);
            PaintHeaderLine(sender, e);
        }
        private void PaintBorder(object sender, PaintEventArgs e)
        {
            Color c = Global.GetColor(Global.MonthPageCalendarBorderColor[0]);
            Pen p = new Pen(c);
            Point p1 = new Point(0, 0);
            Point p2 = new Point(Width - 1, 0);
            e.Graphics.DrawLine(p, p1, p2);
            p1 = new Point(p2.X, Height - 1);
            e.Graphics.DrawLine(p, p1, p2);
            p2 = new Point(0, p1.Y);
            e.Graphics.DrawLine(p, p1, p2);
            p1 = new Point(0, 0);
            e.Graphics.DrawLine(p, p1, p2);
        }
        private void PaintMenuBar(object sender, PaintEventArgs e)
        {
            Color c = Global.GetColor(Global.MonthPageCalendarBorderColor[0]);
            SolidBrush b = new SolidBrush(c);
            Point p = new Point(2, 2);
            Size s = new Size(Width - 5, 22);
            e.Graphics.FillRectangle(b, new Rectangle(p, s));
        }
        private void PaintButton(object sender, PaintEventArgs e)
        {
            Color c = Global.GetColor(Global.UCButtonBorderDark);
            Pen p = new Pen(c);
            Point[] area = new Point[3];
            Point p1 = new Point(6, 4);
            buttonRange[0] = new Rectangle(p1, new Size(15, 16));
            e.Graphics.DrawRectangle(p, buttonRange[0]);

            SolidBrush b = new SolidBrush(c);
            area[0] = new Point(p1.X + 4, p1.Y + 8);
            area[1] = new Point(area[0].X + 3, area[0].Y - 3);
            area[2] = new Point(area[0].X + 3, area[0].Y + 3);
            e.Graphics.FillPolygon(b, area);
            area[0].Offset(4, 0);
            area[1].Offset(4, 0);
            area[2].Offset(4, 0);
            e.Graphics.FillPolygon(b, area);

            p1.Offset(17, 0);
            buttonRange[1] = new Rectangle(p1, new Size(15, 16));
            e.Graphics.DrawRectangle(p, buttonRange[1]);

            area[0] = new Point(p1.X + 5, p1.Y + 8);
            area[1] = new Point(area[0].X + 6, area[0].Y - 3);
            area[2] = new Point(area[0].X + 6, area[0].Y + 3);
            e.Graphics.FillPolygon(b, area);

            p1.Offset(50, 0);
            buttonRange[2] = new Rectangle(p1, new Size(15, 16));
            e.Graphics.DrawRectangle(p, buttonRange[2]);

            area[0] = new Point(p1.X + 11, p1.Y + 8);
            area[1] = new Point(area[0].X - 6, area[0].Y - 3);
            area[2] = new Point(area[0].X - 6, area[0].Y + 3);
            e.Graphics.FillPolygon(b, area);

            p1.Offset(17, 0);
            buttonRange[3] = new Rectangle(p1, new Size(15, 16));
            e.Graphics.DrawRectangle(p, buttonRange[3]);

            area[0] = new Point(p1.X + 12, p1.Y + 8);
            area[1] = new Point(area[0].X - 3, area[0].Y - 3);
            area[2] = new Point(area[0].X - 3, area[0].Y + 3);
            e.Graphics.FillPolygon(b, area);
            area[0].Offset(-4, 0);
            area[1].Offset(-4, 0);
            area[2].Offset(-4, 0);
            e.Graphics.FillPolygon(b, area);

            p1.Offset(19, 0);
            buttonRange[4] = new Rectangle(p1, new Size(15, 16));
            e.Graphics.DrawRectangle(p, buttonRange[4]);

            area[0] = new Point(p1.X + 5, p1.Y + 8);
            area[1] = new Point(area[0].X + 6, area[0].Y - 3);
            area[2] = new Point(area[0].X + 6, area[0].Y + 3);
            e.Graphics.FillPolygon(b, area);

            p1.Offset(36, 0);
            buttonRange[5] = new Rectangle(p1, new Size(15, 16));
            e.Graphics.DrawRectangle(p, buttonRange[5]);

            area[0] = new Point(p1.X + 11, p1.Y + 8);
            area[1] = new Point(area[0].X - 6, area[0].Y - 3);
            area[2] = new Point(area[0].X - 6, area[0].Y + 3);
            e.Graphics.FillPolygon(b, area);
        }
        private void PaintHeaderLine(object sender, PaintEventArgs e)
        {
            Color c = Global.GetColor(Global.LogoBorderColor1);
            Pen p = new Pen(c);
            Point p1 = new Point(3, 40);
            Point p2 = new Point(Width - 4, 40);
            e.Graphics.DrawLine(p, p1, p2);
        }
        private void PaintMonthDate(Label l, DateTime d, bool isThisMonth)
        {
            DateType dt;
            if (!Global.designMode)
                dt = Calendar.GetDateType(Global.country, d);
            else
                dt = DateType.Workday;

            l.Text = d.Day.ToString();
            l.Tag = d.ToShortDateString();
            if (isThisMonth)
                if (d.Date == DateTime.Now.Date)
                    l.Font = new Font(l.Font.FontFamily, 9, FontStyle.Bold);
                else
                    l.Font = new Font(l.Font.FontFamily, 9);
            else
                l.Font = new Font(l.Font.FontFamily, 8);

            if (dt == DateType.Holiday || dt == DateType.Sunday)

                l.ForeColor = Global.GetColor(Global.MonthPageDateForeColorSunday);
            else if (dt == DateType.Holiday || dt == DateType.Saturday)
                l.ForeColor = Global.GetColor(Global.MonthPageDateForeColorSaturday);
            else if (d.Date == SelectedDate.Date)
                l.ForeColor = Color.White;
            else if (dt == DateType.Holiday || dt == DateType.Workday)
                l.ForeColor = Global.GetColor(Global.MonthPageDateForeColorWorkday);

            if (d.Date == SelectedDate.Date)
                l.BackColor = Color.Gray;
            else
                l.BackColor = Color.White;
        }
        #endregion

        // Business Logic *******************************************************************
        #region Business Logic
        private void SetYearMonth()
        {
            lbYear.Text = Year.ToString();
            lbMonth.Text = Month.ToString();
            SetMonthDate();
        }
        private void SetMonthDate()
        {
            for (int row = 0; row < 6; row++)
            {
                for (int col = 0; col < 7; col++)
                {
                    DateTime d = Calendar.GetDateTime(Year, Month, row, col);
                    if (d.Month == Month)
                        PaintMonthDate(lbMonthDate[row, col], d, true);
                    else
                        PaintMonthDate(lbMonthDate[row, col], d, false);
                }
            }
        }
        private void UCSelectDay_Click(object sender, EventArgs e)
        {
            Point p = PointToClient(Cursor.Position);

            if (buttonRange[0].Contains(p))
            {
                Year = Year - 10;
            }
            else if (buttonRange[1].Contains(p))
            {
                Year = Year - 1;
            }
            else if (buttonRange[2].Contains(p))
            {
                Year = Year + 1;
            }
            else if (buttonRange[3].Contains(p))
            {
                Year = Year + 10;
            }
            else if (buttonRange[4].Contains(p))
            {
                if (Month == 1)
                {
                    Year = Year - 1;
                    Month = 12;
                }
                else
                {
                    Month = Month - 1;
                }
            }
            else if (buttonRange[5].Contains(p))
            {
                if (Month == 12)
                {
                    Year = Year + 1;
                    Month = 1;
                }
                else
                {
                    Month = Month + 1;
                }
            }
        }
        private void LBMonthDate_Click(Object sender, EventArgs e)
        {
            String strDate = ((Label)sender).Tag.ToString();

            if (strDate.Length > 0)
            {
                String[] p = strDate.Split('-');
                DateTime d = new DateTime(Int16.Parse(p[0]),
                                          Int16.Parse(p[1]),
                                          Int16.Parse(p[2]));
                SelectedDate = d;
                DateSelected(EventArgs.Empty);
            }
        }

        public delegate void EventHandler(object sender, EventArgs e, DateTime d);
        public event EventHandler OnDateSelected;
        protected virtual void DateSelected(EventArgs e)
        {
            OnDateSelected(this, e, SelectedDate);
        }
        #endregion
    }
}
